import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;

public class PianoInterface extends JPanel {
    private JSClient client;
    
    public PianoInterface(JSClient aClient) {
        this.client = aClient;
        
        this.setLayout(new GridLayout(1,12));
        // init Piano Keys
        String[] keylabels = {"Glow", "A", "Bb", "B", "C", "C#", "D", "Eb", "E", "F", "F#", "G"};
        for(int i=0; i < 12; i++) {
            JButton key = new JButton(keylabels[i]);
            key.setActionCommand(Integer.toString(i));
            //key.addActionListener(this);
            key.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    JButton source = (JButton)e.getSource();
                    ButtonModel sourceModel = source.getModel();
                    // Button press
                    if(sourceModel.isPressed()){
                        String command = source.getActionCommand();
                        String msg = "NOTE " + client.getUser().getUserid() + " " + client.getUser().getUsername() + " 1 " + client.getUser().getInstrument() + " " + command + " true";
                        client.sendMsg(msg);
                    }
                    // Button release
                    else if(!sourceModel.isPressed()) {
                        String command = source.getActionCommand();
                        String msg = "NOTE " + client.getUser().getUserid() + " " + client.getUser().getUsername() + " 1 " + client.getUser().getInstrument() + " " + command + " false";
                        client.sendMsg(msg);
                    }
                }
            });
            add(key);
        }
        this.setVisible(true);
    }
}
